<script setup lang="ts">
import { useRouter } from 'vue-router';
import {login} from '../api/users.js'
import { ref } from 'vue';
import { jwtDecode } from "jwt-decode";
import { userStore } from '@/stores/userStoret.js';

const router = useRouter()

const userName = ref("")
const password = ref("")
const error = ref()



//登录
async function loginUser() {
  const user=ref({
  userName:userName.value,
  password:password.value
})
  console.log(user.value)
  const response = await login(user.value)
  console.log(response)
  if(response.data.code == 200){
    const token = response.data.data
    const decoded = jwtDecode(token)
    const userId = decoded.userId
    console.log("---------------------")
    console.log(decoded)
    //更新store
    userStore.logged = true 
    userStore.userId = userId
    userStore.userName = userName.value
    userStore.token = token
    console.log("登陆成功")
    //存储一份到localStorage中
    localStorage.setItem("userId",userId)
    localStorage.setItem("userName",userName.value)
    localStorage.setItem("token",token)
    console.log("用户"+ userName.value +"登录成功，token = " + token)   
    router.push(`/`)
  }else{
    confirm("账号或密码错误！")
  }
} 

</script>

<template>
  <div class="container">
    <div class="login">
      <div class="imgbox">
        <img src="/images/logo.png" />
      </div>
      <div class="textbox">
        <h3>welcome</h3>
        <input type="text" v-model="userName" placeholder="请输入账号"/>
        <input type="password" v-model="password" placeholder="请输入密码" />

        <!-- 按钮组 -->
        <div class="button-group">
          <input type="submit" @click="loginUser()" value="登录"/>
          <router-link to="/signUp" class="register-btn">去注册</router-link>
        </div>

      </div>
    </div>
  </div>
</template>

<style scoped>
*{
  margin: 0;
  padding: 0;
  list-style: none;
}

.container{
  width: 100vw;
  height: 100vh;
  display: flex;						
  justify-content: center;			
  align-items: center;				
  transition: 2s;
  background-image: url(../images/pc_login_bg.png) ;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}


.login{
  width: 700px;
  height: 360px;
  background: rgba(255,255,255,0.2);
  border-left:2px solid rgba(255,255,255,0.7);
  border-top:2px solid rgba(255,255,255,0.7);
  border-radius:20px ;
  backdrop-filter:blur(5px);			
  box-shadow: 5px 5px 10px  rgba(0,0,0,0.2);
  display: flex;
  justify-content: center;
  align-items: center;
}

.imgbox img{
  width: 150px;
  border-radius: 50%;
  filter: saturate(80%);
}

.textbox{
  margin-left: 30px;
}

.textbox > * {
  display: block;
  margin-bottom: 10px;
}

.textbox input[type="text"],
.textbox input[type="password"]{
  width: 300px;
  height: 36px;
  outline: none;
  border-radius: 5px;
  border: 0;
  font-size: 16px;
  padding-left: 10px;
  box-sizing: border-box;
}

.textbox h3{
  font-size: 22px;
  color: #CCCCCC;
}

/* 按钮组 */
.button-group{
  display: flex;
  align-items: center;
  margin-top: 10px;
}

.textbox input[type="submit"]{
  width: 150px;
  height: 40px;
  color: #FFFFFF;
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.838);
  border: none;
  border-radius: 5px;
  margin-right: 10px;
  transition: 0.3s;
}

.textbox input[type="submit"]:hover{
  background-color: rgba(0,0,0,1);
}

.register-btn{
  display: inline-block;
  text-align: center;
  width: 120px;
  height: 40px;
  line-height: 40px;
  background-color: rgba(255, 125, 0, 0.8);
  color: #fff;
  border-radius: 5px;
  text-decoration: none;
  transition: 0.3s;
}

.register-btn:hover{
  background-color: rgba(255, 125, 0, 1);
}
</style>
